﻿namespace Smart.Navigation.Windows.ViewModels
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Interactivity;

    /// <summary>
    /// ナビゲーションコンテナビヘイビア
    /// </summary>
    public class NavigationContainerBehavior : Behavior<ContentControl>
    {
        public static readonly DependencyProperty StartPageProperty =
            DependencyProperty.Register("StartPage", typeof(object), typeof(NavigationContainerBehavior), new PropertyMetadata(null));

        public static readonly DependencyProperty NavigatorFactoryProperty =
            DependencyProperty.Register("NavigatorFactory", typeof(INavigatorFactory), typeof(NavigationContainerBehavior), new PropertyMetadata(null));

        /// <summary>
        /// 初期ページ
        /// </summary>
        public object StartPage
        {
            get { return GetValue(StartPageProperty); }
            set { SetValue(StartPageProperty, value); }
        }

        /// <summary>
        /// 
        /// </summary>
        public INavigatorFactory NavigatorFactory
        {
            get { return (INavigatorFactory)GetValue(NavigatorFactoryProperty); }
            set { SetValue(NavigatorFactoryProperty, value); }
        }

        private Navigator navigator;

        /// <summary>
        /// アタッチ
        /// </summary>
        protected override void OnAttached()
        {
            navigator = NavigatorFactory.Create(AssociatedObject);

            if (StartPage != null)
            {
                navigator.Forward(StartPage);
            }
        }
    }
}
